package com.hsjk.szwj.module.train.controller.admin;

import com.hsjk.szwj.framework.common.pojo.CommonResult;
import com.hsjk.szwj.framework.common.pojo.PageResult;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminCreateProductSpuReqVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminProductSpuDetailRespVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminProductSpuPageReqVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminProductSpuPageRespVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminUpdateProductSpuReqVO;
import com.hsjk.szwj.module.train.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.hsjk.szwj.framework.common.pojo.CommonResult.success;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/09/10 01:07
 **/
@Tag(name = "后台管理 - 商品Spu")
@RestController
@RequestMapping("/train/product-spu")
@Validated
@Slf4j
public class AdminProductSpuController {
    @Resource
    private IProductService productService;

    @GetMapping("/page")
    @Operation(summary = "获取商品Spu分页列表")
    @PreAuthorize("@ss.hasPermission('train:product-spu:query')")
    public CommonResult<PageResult<AdminProductSpuPageRespVO>> getProductSpuPageList(@Valid AdminProductSpuPageReqVO reqVO) {
        return success(productService.getProductSpuPageList(reqVO));
    }

    @GetMapping("/get-detail-by-id")
    @Operation(summary = "根据id获取商品spu详情")
    @PreAuthorize("@ss.hasPermission('train:product-spu:query')")
    public CommonResult<AdminProductSpuDetailRespVO> getProductSpuDetailById(@RequestParam("spuId") Long spuId) {
        return success(productService.getProductSpuDetailById(spuId));
    }

    @PostMapping("/create")
    @Operation(summary = "创建商品Spu")
    @PreAuthorize("@ss.hasPermission('train:product-spu:create')")
    public CommonResult<Boolean> createProductSpu(@RequestBody @Valid AdminCreateProductSpuReqVO reqVO) {
        log.info("createProductSpu reqVO:{}", reqVO);
        return success(productService.createProductSpu(reqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新商品Spu")
    @PreAuthorize("@ss.hasPermission('train:product-spu:update')")
    public CommonResult<Boolean> updateProductSpu(@RequestBody @Valid AdminUpdateProductSpuReqVO reqVO) {
        return success(productService.updateProductSpu(reqVO));
    }
}
